package org.tgcloud.group.core.fileUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.tgcloud.group.core.ExportUtils.ExcelUtils;
import org.tgcloud.group.core.GlobalStatic;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.core.fileUtils.waterMarkFileUtils.CustomWaterMarkHandler;
import org.tgcloud.group.core.fileUtils.waterMarkFileUtils.WaterMark;
import org.tgcloud.group.core.fileUtils.waterMarkFileUtils.picUtil.CustomImageModifyHandler;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: zhy
 * @create: 2023-08-25 14:56
 * @description:
 */
@Slf4j
public class FileUtil {

    /**
     * @author: zhy
     * @description: 创建文件并加水印
     * @create: 2023年08月25日 15:29
     * @param data 数据列表
     * @param waterMarkContent 水印文字
     * @param fileTitleName 文件名称
     * @param classObject 数据的对象名称
     * @return
     */
    public static void doWaterMarkReader(List data, String waterMarkContent, String fileTitleName, Class classObject) throws Exception{
        WaterMark watermark = new WaterMark();
        watermark.setContent(waterMarkContent);
        watermark.setWidth(400);
        watermark.setHeight(400);
        watermark.setYAxis(200);
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 注意文件是要.xlsx
        //     String fileName ="D:\\桌面\\download_file\\" + fileTitleName + ".xlsx";
//        String fileName ="/home/checkorder/download_file/" + fileTitleName + ".xlsx";
        String fileName = ExcelUtils.doFolderFilePath(GlobalStatic.downloadPath) + File.separator + fileTitleName + ".xlsx";

        String key = "need_del_app_show_list";
        List<String> list = new ArrayList<>();
        list.add(fileName);
        RedisUtil.setCacheObject(key, list);


        // 设置单元格样式
//        List<CellStyleModel> cellStyleList = new ArrayList<>();
//        //设置单元格字体（黑体）,单元格大小 18号,字体颜色红色,加粗,斜体，下划线,上标,删除线
//        cellStyleList.add(CellStyleModel.createFontCellStyleModel("sheet1", 0, 0, "黑体", 18D, IndexedColors.RED
//                , true, true, Font.U_SINGLE, Font.SS_SUPER, true));
//        //设置单元格背景颜色
////        HSSFColor hssfColor = setColor(workbook, (byte) 153, (byte) 204, (byte) 255);
////        cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("sheet1", 1, 1, hssfColor.getIndex()));
//        cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("sheet1", 1, 1, IndexedColors.SKY_BLUE));
//        //设置单元格边框类型和边框颜色
//        cellStyleList.add(CellStyleModel.createBorderCellStyleModel("sheet1", 0, 2, BorderStyle.DOUBLE, IndexedColors.RED));
//        //设置对齐方式
//        cellStyleList.add(CellStyleModel.createAlignmentCellStyleModel("sheet1", 0, 3, HorizontalAlignment.RIGHT, VerticalAlignment.BOTTOM));
//        //设置自动换行
//        cellStyleList.add(CellStyleModel.createWrapTextCellStyleModel("sheet1", 1, 0, true));


        //导出
        EasyExcel.write(fileName, classObject)
                .inMemory(true)
                .sheet("sheet1")
//                .registerWriteHandler(new CustomCellStyleHandler(cellStyleList))
                .registerWriteHandler(new CustomImageModifyHandler())
                .registerWriteHandler(new CustomWaterMarkHandler(watermark))
                .doWrite(data);


    }


    /**
     * 设置颜色
     *
     * @param workbook 工作簿对象
     * @param r 红色分量值
     * @param g 绿色分量值
     * @param b 蓝色分量值
     * @return HSSFColor对象
     */
    public static HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b){
        HSSFPalette palette = workbook.getCustomPalette();
        HSSFColor hssfColor = null;
        try {
            hssfColor= palette.findColor(r, g, b);
            if (hssfColor == null ){
                palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LAVENDER.getIndex(), r, g,b);
                hssfColor = palette.getColor(HSSFColor.HSSFColorPredefined.LAVENDER.getIndex());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return hssfColor;
    }


}
